<template>
  <div class="welcome">
    <div class="welcome">欢迎注册博客</div>
    <div class="welcome">每一天，乐在沟通。</div>
    <el-card style="width: 600px;height: 100%;margin: 0 auto;
background-color: rgba(255,255,255,0.6)">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="deo-ruleForm"
               style="width: 400px; height:100%;margin: 30px auto;">

        <el-form-item>
          <p style="font-size: 30px;color: #ffffff;position: relative;right:260px;bottom: 30px;">用户注册</p>
          <table style="width: 40px;">
            <tr>
              <td>
                <el-input placeholder="账号" v-model="ruleForm.username" maxlength="15"></el-input>
              </td>
            </tr>

            <tr>
              <td>
                <el-input type="password" placeholder="密码" v-model="ruleForm.password" maxlength="8"
                          style="margin-top: 10px"></el-input>
              </td>
            </tr>

            <tr>
              <td>
                <el-input placeholder="昵称" v-model="ruleForm.nick" maxlength="15" style="margin-top: 10px"></el-input>
              </td>
            </tr>

            <tr>
              <td>
                <el-input
                    placeholder="手机号"
                    v-model="ruleForm.phone"
                    maxlength="11"
                    class="input-with-select"
                    style="border:1px solid black;margin-top: 10px;width: 250px;"
                >
                  <el-button slot="append" v-if="isSend" style="color: black">{{ timeCount }}</el-button>
                  <el-button slot="append" icon="el-icon-chat-dot-square" v-else @click="sendSms"></el-button>
                </el-input>
              </td>
            </tr>

            <tr>
              <td>
                <el-input
                    placeholder="验证码"
                    v-model="smsCode"
                    class="input-with-select"
                    style="border:1px solid black;margin-top: 10px;width: 250px;"
                >
                </el-input>
              </td>
            </tr>


            <tr>
              <el-switch
                  v-model="ruleForm.gender"
                  active-color="#F56C6C"
                  inactive-color="#409EFF"
                  :active-value="0"
                  :inactive-value="1"
                  active-text="女"
                  inactive-text="男"
                  style="bottom: 1px;position: relative;right: 100px "
              ></el-switch>
            </tr>

            <tr>
              <td>
                <el-button  style="margin-top: 10px" type="primary" @click="submitForm()">注册</el-button>
                <el-button style="margin-top: 10px" @click="resetForm('ruleForm')">重置</el-button>
              </td>
            </tr>
          </table>
        </el-form-item>

      </el-form>

    </el-card>
  </div>
</template>


<script>


export default {
  data() {

    return {
      ruleForm: {
        username: '',
        password: '',
        nick:'',
        gender: 1,
        status: 1,
        phone: '',
        smsCode: ''

      },
      //添加设置
      // phone: '',
      smsCode: '',
      sendMsg: '发送短信',
      timeCount: 60,
      isSend: false,
      intervalBtn:{}, //定时器


      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {min: 11, max: 11, message: '长度在11 个字符', trigger: 'blur'}
        ],
      },      //验证规则
      genderClick: true,               // 性别
      AddOrSetByUserId: 0,              // 区别方法【添加 ro 修改】

      dialogVisibleTitle: '添加用户',   // 弹框标题---【添加 or 修改功能】
      dialogVisible: false,          // 弹出框状态【添加 or 修改功能】，默认隐藏
      noneBtnImg: false, // 隐藏上传按钮


    };
  },

  methods: {
    submitForm() {
      let addOrSetData = this.ruleForm;


      if (addOrSetData.username === null || addOrSetData.username === undefined || addOrSetData.username === '') {
        this.$message({type: 'error', message: "账号不能为空"})
        return;
      }
      if (addOrSetData.password === null || addOrSetData.password === undefined || addOrSetData.password === '') {
        this.$message({type: 'error', message: "密码不能为空"})
        return;
      }
      if (this.ruleForm.phone === null || this.ruleForm.phone === undefined || this.ruleForm.phone === '') {
        this.$message({type: 'error', message: "电话号码不能为空"})
        return;
      }
      if (this.smsCode === null || this.smsCode === undefined || this.smsCode === '') {
        this.$message({type: 'error', message: "验证码不能为空"})
        return;
      }
      //
      // if (this.AddOrSetByUserId === 0) {
      //   console.log("即将向服务器发送【添加用户】请求，参数：{" +
      //       "username=" + addOrSetData.username + "，" +
      //       "password=" + addOrSetData.password + "，" +
      //       "gender=" + addOrSetData.gender + "，" +
      //       "phone=" + addOrSetData.phone + "，" +
      //       "}")

      let path = "http://localhost:9090/system/users/createUser"
      this.ruleForm.optionsSelect = this.optionsSelectEcho;
      this.axios.post(path, addOrSetData).then((response) => {
        switch (response.data.code) {
          case 200:
            this.$message({
              type: 'success',
              message: "创建成功"
            });
            this.$router.push({
              path:"/login"
            })
            // this.byUsername();
            break;
          default:
            this.$message({
              type: 'error',
              message: response.data.message
            })
            break;
        }
      })
    },

    sendSms() {
      let addOrSetData = this.ruleForm;
      if (addOrSetData.username === null || addOrSetData.username === undefined || addOrSetData.username === '') {
        this.$message({type: 'error', message: "账号不能为空"})
        return;
      }
      if (addOrSetData.password === null || addOrSetData.password === undefined || addOrSetData.password === '') {
        this.$message({type: 'error', message: "密码不能为空"})
        return;
      }
      if (addOrSetData.nick === null || addOrSetData.nick === undefined || addOrSetData.nick === '') {
        this.$message({type: 'error', message: "昵称不能为空"})
        return;
      }
      if (addOrSetData.phone === null || addOrSetData.phone === undefined || addOrSetData.phone === '') {
        this.$message({type: 'error', message: "电话号码不能为空"})
        return;
      }


      let phone = this.ruleForm.phone;
      //验证手机号格式
      if (phone.substr(0, 2) != 13 && phone.substr(0, 2) != 17 &&phone.substr(0, 2) != 18 && phone.substr(0, 2) != 15 && phone.substr(0, 2) != 18 && phone.substr(0, 3) != 157`` || phone.length != 11) {
        alert("手机号格式不正确");
        return;
      }
      //已经发送过短信，就不允许再次发送
      if (this.isSend) {
        return;
      }

      //已经发送过短信，就不允许再次发送
      if (this.isSend) {
        return;
      }
      this.axios.get("http://localhost:9090/sms/sendSmsCode?phoneNumber=" + phone).then(function (response) {
        console.log(response)
        if (response.data.code == 1) {
          this.$message({type: 'success', message: "短信发送成功"})


        } else{
          alert(response.data.message)
          this.$message({type: 'error', message: "短信发送失败，请稍后重试"})
        }
      })
      //改变短信发送状态和按钮状态, 60秒内不重复发送
      this.isSend = true;
      this.changeStyle();
    },
    changeStyle() {
      this.intervalBtn = setInterval(() => {
        if (this.timeCount <= 0) {
          this.isSend = false;
          // 清除定时器
          clearInterval(this.intervalBtn)
          this.sendMsg = "发送短信"
          this.timeCount = 60;
        }
        // 倒计时
        this.timeCount--
      }, 1000)
    },
    checkSms() {
      this.axios.get("http://localhost:9090/sms/checkSmsCode?phoneNumber=" + this.ruleForm.phone + "&smsCode=" + this.smsCode)
          .then(function (response) {
            if (response.data.code == 1) {
              this.$message.success("注册成功")


            } else if (response.data.code == 2) {

            }
          })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },



  }


}


</script>
<style>

.welcome {
  color: #f8bace;
}

body {
  margin: 0;
  background-image: url(" ../assets/hah.jpg");
  background-size: cover;
  text-align: center;
}

img {
  width: 150px;
  align-content: center
}


</style>
